package com.zb.jsp.action.userLoinAction;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zb.jsp.service.LoginService;
import com.zb.jsp.utils.StringUtil;
import com.zb.jsp.vo.UserVo;

public class UserLoginAction extends ActionSupport {

	//声明一个接受管理员账户的vo
	UserVo userVoForManager ;
	//声明一个接收普通用户信息的vo
	UserVo userVoForCommon ;
	
	  HttpServletRequest request = (HttpServletRequest) ActionContext
      .getContext().get(ServletActionContext.HTTP_REQUEST);
	  
	  /**	
	   * 管理员登陆
	   * @return
	   */
	public String userLogin(){
		//实例化LonginService对象
		LoginService loginService = new LoginService();

		//调用loginservice方法验证数据库信息
		Map map = loginService.login(userVoForManager.getUserName(), userVoForManager.getPasswd(),0);		
		
		Object message = map.get("message");
		System.out.println(userVoForManager.getUserNameCn());
		if(StringUtil.isNullOrEmpty(message)){
			//验证成功
			request.getSession().setAttribute("nameCn",map.get("nameCn"));
//			userVoForManager.setUserNameCn((String)map.get("nameCn"));
//			request.getRequestDispatcher("/jsp/admin_index.jsp").forward(request, response);

		}else{
			//登陆失败
			request.setAttribute("message",message );
//			response.sendRedirect(request.getContextPath()+"/index.jsp");
//			request.getRequestDispatcher("/index.jsp").forward(request, response);
			return "userLoginFail";
		}
		
		return "userLoginSuccess";
	}
	
	/**	
	 * 普通用户登陆
	 * @return
	 */
	public String commonUserLogin(){
		//实例化LonginService对象
		LoginService loginService = new LoginService();

		//调用loginservice方法验证数据库信息
		Map map = loginService.login(userVoForCommon.getUserName(), userVoForCommon.getPasswd(),1);		
		
		Object message = map.get("message");

		if(StringUtil.isNullOrEmpty(message)){
			//验证成功
			request.getSession().setAttribute("nameCn",map.get("nameCn"));

		}else{
			//登陆失败
			request.setAttribute("messageCommon",message );
			return "userLoginFail";
		}
		
		return "commonUserLoginSuccess";
	}

	public UserVo getUserVoForManager() {
		return userVoForManager;
	}

	public void setUserVoForManager(UserVo userVoForManager) {
		this.userVoForManager = userVoForManager;
	}

	public UserVo getUserVoForCommon() {
		return userVoForCommon;
	}

	public void setUserVoForCommon(UserVo userVoForCommon) {
		this.userVoForCommon = userVoForCommon;
	}

}
